/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.util;

import com.phonebooksharing.android.activities.media.AudioHandler;

import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

public class CloseableUtil {
    /**
     * Safely closes the cursor given.
     * It must be released to GC manually 
     * 
     * @param c
     */
    public static void safeClose(Cursor c) {
        if(c != null) {
            c.close();
        }
    }
    
    public static void safeClose(AudioHandler ah) {
        if(ah != null) {
            ah.clean();
        }
    }
    
    public static void safeClose(MediaRecorder mr) {
        try {
            if (mr != null) {
//                mr.stop();
                mr.release();
            }
        } catch (RuntimeException r) {
            r.printStackTrace();
        }
    }
    
    public static void safeClose(MediaPlayer mp) {
        try {
            if (mp != null) {
                mp.stop();
                mp.release();
            }
        } catch (RuntimeException r) {
            r.printStackTrace();
        }
    }
}
